// pages/myOrderDetail/index.js
const {
  post,
  get
} = getApp();
Page({
  data: {
    orderType: {
      1: "待付款",
      2: "已支付",
      3: "已发货",
      4: "已收货",
      5: "已完成",
      6: "取消订单",
      7: "交易成功售后中",
      8: "交易成功部分售后中"
    },
    sn: "",
    dataList: [],
    orderInfo: {},
    addressInfo: {},
    showPage: false
  },
  // 初始化加载
  onLoad: function (e) {
    this.setData({
      sn: e.sn
    })
    this.getList();
  },
  // 获取数据
  getList() {
    post("/order/detail", {
      order_sn: this.data.sn
    }).then(res => {
      if (res.result == 1) {
        this.setData({
          dataList: res.data.orderItemList,
          orderInfo: {
            sn: res.data.sn,
            create_time: res.data.create_time,
            order_amount: res.data.order_amount,
            need_pay_money: res.data.need_pay_money,
            status: res.data.status,
            remain_pay_time: res.data.remain_pay_time,
            shipping_amount: res.data.shipping_amount,
            payment_name: res.data.payment_name,
            discount: res.data.discount
          },
          addressInfo: {
            ship_name: res.data.ship_name,
            ship_mobile: res.data.ship_mobile,
            ship_province_name: res.data.ship_province_name,
            ship_city_name: res.data.ship_city_name,
            ship_region_name: res.data.ship_region_name,
            ship_addr: res.data.ship_addr
          },
          showPage: true
        })
      }
    })
  },
  // 支付
  toPlay(e) {
    const {
      sn
    } = e.currentTarget.dataset;
    const _this = this;
    get("/order/pay", {
      order_sn: sn
    }).then(res => {
      if (res.result == 1) {
        wx.requestPayment({
          timeStamp: res.data.timeStamp,
          nonceStr: res.data.nonceStr,
          package: res.data.packageValue,
          signType: res.data.signType,
          paySign: res.data.paySign,
          success(res) {
            _this.onLoad();
            wx.showToast({
              title: '支付成功',
              icon: 'success',
              duration: 2000
            })
            _this.getList();
          },
          fail(res) {
            wx.showToast({
              title: '支付失败',
              icon: 'none',
              duration: 2000
            })
          }
        })
      }
    });
  },
  // 取消订单
  cancelOrder(e) {
    const {
      sn
    } = e.currentTarget.dataset;
    const _this = this;
    wx.showModal({
      content: '您确定取消此订单吗',
      success(res) {
        if (res.confirm) {
          post('/order/cancel', {
            order_sn: sn,
            cancel_reason: ""
          }).then(res => {
            if (res.result == 1) {
              _this.onLoad();
              wx.showToast({
                title: '取消订单成功',
                icon: 'success',
                duration: 2000
              });
              _this.getList();
            }
          })
        }
      }
    })
  },
  // 删除订单
  dedleteOrder(e) {
    const {
      sn
    } = e.currentTarget.dataset;
    const _this = this;
    wx.showModal({
      content: '您确定删除此订单吗？',
      success(res) {
        if (res.confirm) {
          post('/order/delete', {
            order_sn: sn
          }).then(res => {
            if (res.result == 1) {
              _this.onLoad();
              wx.showToast({
                title: '删除成功',
                icon: 'success',
                duration: 2000
              });
              _this.getList();
            }
          })
        }
      }
    })
  },
  // 确认收货
  confirmOrder(e) {
    const _this = this;
    const {
      sn
    } = e.currentTarget.dataset;
    wx.showModal({
      content: '您确定收货此订单吗？',
      success(res) {
        if (res.confirm) {
          post('/order/confirm', {
            order_detail_sn: sn
          }).then(res => {
            if (res.result == 1) {
              wx.showToast({
                title: '已确认收货',
                icon: 'success',
                duration: 2000
              });
              _this.getList();
            }
          })
        }
      }
    })
  },
  // 售后
  afterSales(e) {
    const {
      sn
    } = e.currentTarget.dataset;
    post("/order/after-sale", {
      order_detail_sn: sn
    }).then(res => {
      if (res.result == 1) {
        wx.makePhoneCall({
          phoneNumber: res.data.tel
        })
      }
    });
  },
  // 查看物流
  goToLogistics(e) {
    const {
      sn
    } = e.currentTarget.dataset;
    wx > wx.navigateTo({
      url: `../logistics/index?sn=${sn}`
    })
  },
  // 查看激活码
  viewActivationCode(e) {
    wx.navigateTo({
      url: `../activationCode/index?sn=${e.currentTarget.dataset.sn}`,
    });
  },
  onShow: function () {
    // 控制分享按钮
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline'],
      success(res) {
        console.log('showShareMenu', res);
      }
    });
  }
})